package o;

import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import com.huawei.appgallery.foundation.storage.db.RecordBean;
import java.util.List;

public class euc extends cfq {
    private static euc ˊ;
    private SQLiteDatabase ˋ = null;
    private Context ॱ;

    protected euc(Context context) {
        super(context, "app.db", null, eud.ˋ());
        this.ॱ = context;
        this.ˋ = getWritableDatabase();
    }

    public static synchronized euc ˊ() {
        euc o_euc;
        synchronized (euc.class) {
            if (ˊ == null) {
                ˊ = new euc(ContentManager.getInstant().getContext());
            }
            o_euc = ˊ;
        }
        return o_euc;
    }

    protected void ॱ(SQLiteDatabase sQLiteDatabase, int i) {
        try {
            this.ˋ = sQLiteDatabase;
            eud o_eud = new eud(this, this.ॱ);
            sQLiteDatabase.beginTransaction();
            o_eud.ˏ();
            if (i != -1) {
                sQLiteDatabase.setVersion(i);
            }
            sQLiteDatabase.setTransactionSuccessful();
        } catch (Throwable e) {
            Logger.ˋ("DbHelper", "DbHelper initTables error. ", e);
        } finally {
            sQLiteDatabase.endTransaction();
        }
    }

    /* JADX WARNING: inconsistent code. */
    /* Code decompiled incorrectly, please refer to instructions dump. */
    public boolean ˋ(java.lang.String r10) throws android.database.SQLException {
        /*
        r9 = this;
        r2 = 1;
        r0 = 0;
        r1 = r9.ʻ(r10);
        if (r1 != 0) goto L_0x000e;
    L_0x0008:
        r0 = new android.database.SQLException;
        r0.<init>();
        throw r0;
    L_0x000e:
        if (r10 != 0) goto L_0x0011;
    L_0x0010:
        return r0;
    L_0x0011:
        r1 = 0;
        r3 = "select count(1) as c from sqlite_master where type ='table' and name = ?";
        r4 = r9.ˋ;	 Catch:{ SQLException -> 0x0038, all -> 0x004c }
        r5 = 1;
        r5 = new java.lang.String[r5];	 Catch:{ SQLException -> 0x0038, all -> 0x004c }
        r6 = 0;
        r7 = r10.trim();	 Catch:{ SQLException -> 0x0038, all -> 0x004c }
        r5[r6] = r7;	 Catch:{ SQLException -> 0x0038, all -> 0x004c }
        r1 = r4.rawQuery(r3, r5);	 Catch:{ SQLException -> 0x0038, all -> 0x004c }
        r3 = r1.moveToNext();	 Catch:{ SQLException -> 0x004e, all -> 0x004c }
        if (r3 == 0) goto L_0x0032;
    L_0x002a:
        r3 = 0;
        r3 = r1.getInt(r3);	 Catch:{ SQLException -> 0x004e, all -> 0x004c }
        if (r3 <= 0) goto L_0x0032;
    L_0x0031:
        r0 = r2;
    L_0x0032:
        if (r1 == 0) goto L_0x0010;
    L_0x0034:
        r1.close();
        goto L_0x0010;
    L_0x0038:
        r0 = move-exception;
        r0 = r1;
    L_0x003a:
        r1 = new android.database.SQLException;	 Catch:{ all -> 0x0042 }
        r2 = "isTableExist SQLException";
        r1.<init>(r2);	 Catch:{ all -> 0x0042 }
        throw r1;	 Catch:{ all -> 0x0042 }
    L_0x0042:
        r1 = move-exception;
        r8 = r1;
        r1 = r0;
        r0 = r8;
    L_0x0046:
        if (r1 == 0) goto L_0x004b;
    L_0x0048:
        r1.close();
    L_0x004b:
        throw r0;
    L_0x004c:
        r0 = move-exception;
        goto L_0x0046;
    L_0x004e:
        r0 = move-exception;
        r0 = r1;
        goto L_0x003a;
        */
        throw new UnsupportedOperationException("Method not decompiled: o.euc.ˋ(java.lang.String):boolean");
    }

    public void ˏ(String str) throws SQLException {
        if (ʻ(str)) {
            try {
                this.ˋ.execSQL(" ALTER TABLE " + str + " RENAME TO _temp_" + str);
                return;
            } catch (SQLException e) {
                Logger.e("DbHelper", "alter table error!");
                throw new SQLException("alter table error!");
            }
        }
        throw new SQLException();
    }

    public void ॱ(String str) throws SQLException {
        ˊ("_temp_" + str);
    }

    public void ˊ(String str) throws SQLException {
        if (ʻ(str)) {
            ˎ(" DROP TABLE " + str);
            return;
        }
        throw new SQLException();
    }

    public void ˎ(String str) throws SQLException {
        try {
            this.ˋ.execSQL(str);
        } catch (SQLException e) {
            Logger.e("DbHelper", " executeSQL error ");
            throw new SQLException("DbHelper executeSQL error ");
        }
    }

    /* JADX WARNING: inconsistent code. */
    /* Code decompiled incorrectly, please refer to instructions dump. */
    public java.lang.String[] ॱॱ(java.lang.String r6) throws android.database.SQLException {
        /*
        r5 = this;
        r0 = 0;
        r1 = r5.ʻ(r6);
        if (r1 != 0) goto L_0x000d;
    L_0x0007:
        r0 = new android.database.SQLException;
        r0.<init>();
        throw r0;
    L_0x000d:
        r1 = new java.lang.StringBuilder;
        r1.<init>();
        r2 = " select * from ";
        r1 = r1.append(r2);
        r1 = r1.append(r6);
        r2 = " order by _id asc LIMIT 1";
        r1 = r1.append(r2);
        r1 = r1.toString();
        r2 = r5.ˋ;	 Catch:{ SQLException -> 0x003a, all -> 0x0054 }
        r3 = 0;
        r0 = r2.rawQuery(r1, r3);	 Catch:{ SQLException -> 0x003a, all -> 0x0054 }
        r0.moveToNext();	 Catch:{ SQLException -> 0x003a }
        r1 = r0.getColumnNames();	 Catch:{ SQLException -> 0x003a }
        if (r0 == 0) goto L_0x0039;
    L_0x0036:
        r0.close();
    L_0x0039:
        return r1;
    L_0x003a:
        r1 = move-exception;
        r1 = "DbHelper";
        r2 = "DbHelper getColumnNames error [] ";
        o.dna.ˊ(r1, r2);	 Catch:{ all -> 0x004a }
        r1 = new android.database.SQLException;	 Catch:{ all -> 0x004a }
        r2 = "getColumnNames error []";
        r1.<init>(r2);	 Catch:{ all -> 0x004a }
        throw r1;	 Catch:{ all -> 0x004a }
    L_0x004a:
        r1 = move-exception;
        r4 = r1;
        r1 = r0;
        r0 = r4;
    L_0x004e:
        if (r1 == 0) goto L_0x0053;
    L_0x0050:
        r1.close();
    L_0x0053:
        throw r0;
    L_0x0054:
        r1 = move-exception;
        r4 = r1;
        r1 = r0;
        r0 = r4;
        goto L_0x004e;
        */
        throw new UnsupportedOperationException("Method not decompiled: o.euc.ॱॱ(java.lang.String):java.lang.String[]");
    }

    public void ˊ(String str, List<RecordBean> list, List<String> list2, List<String[]> list3) {
        Logger.ˎ("DbHelper", "DbHelper:mergeBatch()");
        if (list != null && list2 != null && list3 != null) {
            int size = list.size();
            if (size == list2.size() && size == list3.size()) {
                SQLiteDatabase writableDatabase = getWritableDatabase();
                writableDatabase.beginTransaction();
                int i = 0;
                while (i < size) {
                    try {
                        if (writableDatabase.update(str, ((RecordBean) list.get(i)).J_(), (String) list2.get(i), (String[]) list3.get(i)) <= 0) {
                            writableDatabase.insertOrThrow(str, null, ((RecordBean) list.get(i)).J_());
                        }
                        i++;
                    } catch (Exception e) {
                        Logger.e("DbHelper", "mergeBatch error");
                        return;
                    } finally {
                        writableDatabase.endTransaction();
                    }
                }
                writableDatabase.setTransactionSuccessful();
                writableDatabase.endTransaction();
                return;
            }
            Logger.e("DbHelper", "mergeBatch list size not match!");
        }
    }

    public Cursor ˏ(String str, String[] strArr, String str2, String[] strArr2, String str3) {
        try {
            return getWritableDatabase().query(str, strArr, str2, strArr2, null, null, str3);
        } catch (Exception e) {
            Logger.e("DbHelper", "query ex");
            return null;
        }
    }

    public int ॱ(String str, String str2, String[] strArr) {
        Logger.ˏ("DbHelper", "DbHelper:delete()");
        int i = 0;
        try {
            i = getWritableDatabase().delete(str, str2, strArr);
        } catch (Exception e) {
            Logger.e("DbHelper", "delete ex");
        }
        return i;
    }
}
